package com.xjzd.spring.proxy;

import org.aopalliance.intercept.MethodInterceptor;
import org.springframework.aop.framework.ProxyFactory;

public class ManualCglibProxyDemo {

    public static void main(String[] args) {
        // 目标对象
        UserService target = new UserService();

        // 创建代理工厂
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setTarget(target);
        proxyFactory.setProxyTargetClass(true); // 强制使用 CGLIB

        // 添加增强（Advice）
        proxyFactory.addAdvice((MethodInterceptor) invocation -> {
            System.out.println("[手动代理] 方法调用前");
            Object result = invocation.proceed();
            System.out.println("[手动代理] 方法调用后");
            return result;
        });

        // 获取代理对象
        UserService proxy = (UserService) proxyFactory.getProxy();

        // 调用方法
        proxy.saveUser();
    }
}
